package com.simpleim.starter.protoBuilder;

import com.simpleim.im.common.bean.ChatMsg;
import com.simpleim.im.common.bean.User;
import com.simpleim.im.common.bean.msg.ProtoMsg;
import com.simpleim.starter.client.ClientSession;

import java.util.concurrent.atomic.AtomicInteger;

public class ChatMsgBuilder extends BaseBuilder{

    private ChatMsg chatMsg;

    private User user;

    private static final AtomicInteger messageId = new AtomicInteger(1);

    private ChatMsgBuilder(ChatMsg chatMsg, User user, ClientSession session) {
        super(ProtoMsg.HeadType.MESSAGE_REQUEST, session);
        this.chatMsg = chatMsg;
        this.user = user;
    }

    private ProtoMsg.Message build() {
        ProtoMsg.Message message = buildCommon(-1);
        ProtoMsg.MessageRequest.Builder msgBuilder = ProtoMsg.MessageRequest.newBuilder();
        ProtoMsg.MessageRequest messageRequest = msgBuilder.setTo(chatMsg.getTo())
                .setFrom(chatMsg.getFrom())
                .setContent(chatMsg.getContent())
                .setMessageId(chatMsg.getMsgId())
                .setMessageType(1)
                .setTime(System.currentTimeMillis()).build();
        return message.toBuilder().setSequence(getSeqId())
                .setSessionId(getSession().getSessionId())
                .setType(getHeadType())
                .setMessageRequest(messageRequest).build();

    }

    public static ProtoMsg.Message buildChartMsg(ChatMsg chatMsg, User user, ClientSession session) {
        ChatMsgBuilder msgBuilder = new ChatMsgBuilder(chatMsg, user, session);
        return msgBuilder.build();
    }
}
